﻿namespace Site2AppWP8.Config
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using Site2AppWP8.Modules;

    [DataContract]
    public class RedirectConfig
    {
        private bool? isPopUpEnabled;

        public RedirectConfig()
        {
            this.IsEnabled = false;
            this.Rules = new List<Rule>();
        }

        [DataMember(Name = "enabled")]
        public bool IsEnabled { get; set; }

        public bool IsPopUpEnabled
        {
            get
            {
                if (!this.isPopUpEnabled.HasValue)
                {
                    foreach (var rule in this.Rules)
                    {
                        if (rule.Action.Equals(RedirectModule.PopUpActionName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            this.isPopUpEnabled = true;
                            return true;
                        }
                    }

                    this.isPopUpEnabled = false;
                }

                return this.isPopUpEnabled.Value;
            }
        }

        [DataMember(Name = "rules")]
        public List<Rule> Rules { get; set; }
    }
}
